package com.zmm.weblog.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmm.weblog.admin.model.vo.tag.*;
import com.zmm.weblog.admin.service.AdminTagService;
import com.zmm.weblog.common.domain.dos.ArticleTagRelDo;
import com.zmm.weblog.common.domain.dos.TagDo;
import com.zmm.weblog.common.domain.mapper.ArticleTagRelMapper;
import com.zmm.weblog.common.domain.mapper.TagMapper;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.exception.BizException;
import com.zmm.weblog.common.model.vo.SelectRspVo;
import com.zmm.weblog.common.utils.PageResponse;
import com.zmm.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author:福禄寿
 * @Date:2024/4/30 下午4:14
 * @Des:weblog-springboot
 **/
@Service
@Slf4j
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDo> implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    @Override
    public Response addTag(AddTagReqVo addTagReqVo) {

        //VO 转DO
        List<TagDo> tagDoList = addTagReqVo.getTags().stream().map(tagName -> new TagDo().builder()
                .name(tagName.trim())
                .updateTime(LocalDateTime.now())
                .createTime(LocalDateTime.now())
                .build()).collect(Collectors.toList());

        //批量添加
        try {
            saveBatch(tagDoList);
        } catch (Exception e) {
            log.warn("添加标签失败，失败原因：{}", e.getMessage());
        }
        return Response.success();
    }

    @Override
    public PageResponse findTagPageList(FindTagPageListReqVo pageListReqVo) {
        Long current = pageListReqVo.getCurrent();
        Long size = pageListReqVo.getSize();
        String name = pageListReqVo.getName();
        LocalDate startDate = pageListReqVo.getStartDate();
        LocalDate endDate = pageListReqVo.getEndDate();
        Page<TagDo> tagDoPage = tagMapper.selectPageList(current, size, name, startDate, endDate);
        List<TagDo> tagDos = tagDoPage.getRecords();
        List<FindTagPageListRspVo> vos = null;
        if (!CollectionUtils.isEmpty(tagDos)) {
            vos = tagDos.stream().map(tagDo -> new FindTagPageListRspVo().builder()
                    .id(tagDo.getId())
                    .name(tagDo.getName())
                    .createTime(tagDo.getCreateTime())
                    .build()).collect(Collectors.toList());
        }
        return PageResponse.success(tagDoPage, vos);
    }

    @Override
    public Response deleteTag(DeleteTagReqVo tagReqVo) {
        ArticleTagRelDo articleTagRelDO = articleTagRelMapper.selectByTagId(tagReqVo.getId());
        if (articleTagRelDO != null){
            log.warn("删除标签失败，标签被文章使用，文章ID");
            throw new BizException(ResponseCodeEnum.TAG_CAN_NOT_DELETE);
        }
        tagMapper.deleteById(tagReqVo.getId());
        int count = tagMapper.deleteById(tagReqVo.getId());
        return count > 0 ? Response.success() : Response.fail(ResponseCodeEnum.TAG_NOT_EXISTED);
    }

    @Override
    public Response selectListTag() {
        List<TagDo> tagDos = tagMapper.selectList(null);

        List<SelectRspVo> vos = null;
        if (!CollectionUtils.isEmpty(tagDos)){
            vos = tagDos.stream().map(tagDo -> new SelectRspVo().builder()
                    .label(tagDo.getName())
                    .value(tagDo.getId())
                    .build()).collect(Collectors.toList());
        }
        return Response.success(vos);
    }

    @Override
    public Response selectTag(SearchTagListReqVo searchTagReqVo) {
        List<TagDo> tagDos = tagMapper.selectByName(searchTagReqVo.getName());
       List<SelectRspVo> vos =null;
        if (!CollectionUtils.isEmpty(tagDos)){
            vos = tagDos.stream().map(tagDo -> new SelectRspVo().builder()
                    .label(tagDo.getName())
                    .build()).collect(Collectors.toList());
        }

        return Response.success(vos);
    }
}